import axios from 'axios'


let myaxios = axios.create({
    timeout: 15000,
    // withCredentials: true,
    baseURL:process.env.VUE_APP_BASE_URL
})

myaxios.interceptors.request.use(requestConfig => {
    let tokenObj = localStorage.getItem("JWT")
    if (tokenObj) {
        requestConfig.headers["Authorization"] = `${tokenObj}`
    }
    return requestConfig;
})

function showErro(error:string){
    alert(error)
}

myaxios.interceptors.response.use(function (response) {
    return response;
}, function (error) {
    
    if (error.response == undefined) {
        showErro('服务器连接超时')
        return;
    }
    let code = error.response.status;

    if (code >= 500) {
        let data = error.response.data;
        showErro(`服务器发生错误：${data.message}`)
    }

    if (code === 401) {
        //goTo(ssoUrl);
        
        window.location.href = process.env.VUE_APP_SSO_URL;
    }

    if (code === 403) {
        showErro(`没有权限进行此操作`)
    }

    if (code === 400) {
        let data = error.response.data;
        showErro(`输入错误`)
    }

    // Do something with response error
    return Promise.reject(error);
})

export default myaxios